/*
 * IncomeFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.pojo.Category;
import com.insigma.yanwenlong.bookkeeping.pojo.Detail;
import com.insigma.yanwenlong.bookkeeping.pojo.Member;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.CategoryService;
import com.insigma.yanwenlong.bookkeeping.service.CategoryServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.DetailService;
import com.insigma.yanwenlong.bookkeeping.service.DetailServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.MemberService;
import com.insigma.yanwenlong.bookkeeping.service.MemberServiceImpl;
import com.insigma.yanwenlong.bookkeeping.util.GetNowTime;
import com.insigma.yanwenlong.bookkeeping.util.StringToDate;

/**
 *
 * @author  __USER__
 */
public class IncomeOrOutcomeFrame extends javax.swing.JFrame {

	private JButton btn;
	private CategoryService categoryService = new CategoryServiceImpl();
	private AccountService accountService = new AccountServiceImpl();
	private MemberService memberService = new MemberServiceImpl();
	private DetailService detailService = new DetailServiceImpl();

	/** Creates new form IncomeFrame */
	public IncomeOrOutcomeFrame() {
		initComponents();
		//居中
		setLocationRelativeTo(null);
		//关闭此窗体而不关闭父窗体
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public IncomeOrOutcomeFrame(JButton btn) {
		this();
		this.btn = btn;
		//初始化界面上的数据
		initData(btn);
	}

	private void initData(JButton btn) {
		jtfDate.setText(new GetNowTime().getNowTime());
		if (btn.getText().equals("支出")) {
			try {
				List<Category> categories = categoryService.findCategories();
				DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobCategory
						.getModel();
				for (Category category : categories) {
					if (category.getDirectionId() == 1) {
						dcbm.addElement(category);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} else if (btn.getText().equals("收入")) {
			try {
				List<Category> categories = categoryService.findCategories();
				DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobCategory
						.getModel();
				for (Category category : categories) {
					if (category.getDirectionId() == 2) {
						dcbm.addElement(category);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//添加账户
		try {
			List<Account> accounts = accountService.findaAccounts();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobAccount
					.getModel();
			for (Account account : accounts) {
				dcbm.addElement(account);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		//添加家庭成员
		try {
			List<Member> members = memberService.findallMembers();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobMember
					.getModel();
			for (Member member : members) {
				dcbm.addElement(member);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jtfDate = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		cobCategory = new javax.swing.JComboBox();
		jLabel3 = new javax.swing.JLabel();
		spnHappenMoney = new javax.swing.JSpinner();
		cobAccount = new javax.swing.JComboBox();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		cobMember = new javax.swing.JComboBox();
		jLabel6 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jtaNote = new javax.swing.JTextArea();
		btnCommit = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u65e5\u671f");

		jLabel2.setText("\u7c7b\u522b");

		jLabel3.setText("\u53d1\u751f\u91d1\u989d");

		spnHappenMoney.setModel(new javax.swing.SpinnerNumberModel(Double
				.valueOf(0.0d), Double.valueOf(0.0d), null, Double
				.valueOf(100.0d)));

		jLabel4.setText("\u8d26\u6237");

		jLabel5.setText("\u5bb6\u5ead\u6210\u5458");

		jLabel6.setText("\u8bf4\u660e");

		jtaNote.setColumns(20);
		jtaNote.setRows(5);
		jScrollPane1.setViewportView(jtaNote);

		btnCommit.setText("\u63d0\u4ea4");
		btnCommit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnCommitActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGap(
																												20,
																												20,
																												20)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																jLabel1)
																														.addComponent(
																																jLabel2)))
																						.addComponent(
																								jLabel5)
																						.addComponent(
																								jLabel3,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								56,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				28,
																				28,
																				28)
																		.addComponent(
																				jLabel4))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				26,
																				26,
																				26)
																		.addComponent(
																				jLabel6)))
										.addGap(24, 24, 24)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				111,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				cobAccount,
																				0,
																				111,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																		.addComponent(
																				cobCategory,
																				0,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				cobMember,
																				0,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				jtfDate,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				113,
																				Short.MAX_VALUE)
																		.addComponent(
																				spnHappenMoney,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				94,
																				Short.MAX_VALUE))))
						.addGroup(
								layout.createSequentialGroup().addGap(59, 59,
										59).addComponent(btnCommit)
										.addContainerGap(95, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jtfDate,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(17, 17, 17)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																cobCategory,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel2))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																spnHappenMoney,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel3))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																cobAccount,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(22, 22, 22)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																cobMember,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(27, 27, 27)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																53,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6))
										.addGap(29, 29, 29)
										.addComponent(btnCommit)
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//点击提交，插入detail信息
	private void btnCommitActionPerformed(java.awt.event.ActionEvent evt) {
		Account account = (Account) cobAccount.getSelectedItem();
		int accountId = account.getId();
		Date tranTime = StringToDate.getDate(jtfDate.getText());
		Category category = (Category) cobCategory.getSelectedItem();
		int categoryId = category.getId();
		double happenMoney = (Double) spnHappenMoney.getValue();
		Member member = (Member) cobMember.getSelectedItem();
		int memberId = member.getId();
		String note = jtaNote.getText();

		Detail detail = new Detail();
		detail.setAccountId(accountId);
		detail.setCategoryId(categoryId);
		detail.setHappenMoney(happenMoney);
		detail.setMemberId(memberId);
		detail.setTranTime(tranTime);
		detail.setNote(note);

		try {
			int rowAffected = detailService.addDetail(detail);
			if (rowAffected > 0) {
				JOptionPane.showMessageDialog(this, "插入成功");
			} else {
				JOptionPane.showMessageDialog(this, "插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new IncomeOrOutcomeFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnCommit;
	private javax.swing.JComboBox cobAccount;
	private javax.swing.JComboBox cobCategory;
	private javax.swing.JComboBox cobMember;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jtaNote;
	private javax.swing.JTextField jtfDate;
	private javax.swing.JSpinner spnHappenMoney;
	// End of variables declaration//GEN-END:variables

}